package core.tree;

import core.utils.Utils;
import java.awt.Color;
import java.util.LinkedList;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Tree{

    public static int INITIAL_TREE_MAG = 4000;
    public double mag;
    protected Vertex root;
    private double angle;
    public Tree(Vertex r, double m){
        this.root =  r;
        angle = 0.00;
        mag = m;
    }
    public void setMagn(double m){mag = m;}
    public Vertex getRoot(){return root;}
    public int getNumberOfLeaves(){return root.getNoLeaf();}
    public void setCoords(int rootX, int rootY){
        root.setX(rootX);
        root.setY(rootY);
        setCoords(root, 0.0);
    }

    private int start, end;
    public void setStart(int s){start = s;}
    public void setEnd(int e){end = e;}
    public int getStart(){return start;}
    public int getEnd(){return end;}

    public void setCircularStartAngle(double a){angle = a;}

    public void setVertexCoordinatesCircular(int rootX, int rootY){
        this.root.setX(rootX);
        this.root.setY(rootY);
        setCoords(this.root, angle);
    }

    private void setCoords(Vertex parent, double theta){
        double noOfDescendents = root.getNoLeaf();
        double rotUnit; double radius; double childAngle; double lastRot = 0.00;
        double angleToRot; double fullAngle;
        Object [] children = parent.getChildren().toArray();
        double expSpace = 0.00;
        if(parent.getNoLeaf()>0){expSpace = ((6.283185307/noOfDescendents) * parent.getNoLeaf());}
        else{expSpace = (6.283185307/noOfDescendents) * parent.getNoLeaf();}
        rotUnit = (expSpace/parent.getNoLeaf());
        for(int i=0; i!=children.length; i++){
            Vertex child = (Vertex)children[i];
            angleToRot = 0.00; fullAngle = 0.00;
            if(child.getNoLeaf() == 0){
                angleToRot = lastRot + (rotUnit/2);
                fullAngle = lastRot + rotUnit;
            }
            else{
                angleToRot = lastRot + (rotUnit * child.getNoLeaf())/2;
                fullAngle = lastRot + (rotUnit * child.getNoLeaf());
            }
            lastRot = fullAngle;
            childAngle =  theta - (expSpace/2)  +  (angleToRot);
            radius = child.getDistToParent() * mag;
            child.setX(child.ptrToParent().getX() + (int)(Math.cos(childAngle) * radius));
            child.setY(child.ptrToParent().getY() + (int)(Math.sin(childAngle) * radius));
            if(child.getNoOfChildren()>0){setCoords(child, childAngle);}
        }
    }

    public void setNodeColAndSize() {
        colors = Utils.getColors((int)NO_OF_COLORS);
        LinkedList <Vertex> leafs = this.getRoot().getLeafList();
        maxFreq = 0;
        for(int x=0; x<leafs.size(); x++){
            int freq;
            if(leafs.get(x).getTitle().indexOf("_GENO")==-1){freq = new Integer(leafs.get(x).getTitle().split("_FREQ")[1]).intValue();}
            else{freq = new Integer(leafs.get(x).getTitle().split("_FREQ")[1].split("_GENO")[0]).intValue();}
            if(maxFreq<freq){maxFreq = freq;}
        }

        for(int x=0; x<leafs.size(); x++){
            int freq;
            if(leafs.get(x).getTitle().indexOf("_GENO")==-1){freq = new Integer(leafs.get(x).getTitle().split("_FREQ")[1]).intValue();}
            else{freq = new Integer(leafs.get(x).getTitle().split("_FREQ")[1].split("_GENO")[0]).intValue();}
            double percent = (int)(((double)freq / (double)maxFreq) * 100.00);
            leafs.get(x).setCols(colors.get((int)(percent)).getRed(), colors.get((int)(percent)).getGreen(), colors.get((int)(percent)).getBlue());
        }
    }

    public int maxFreq = 0;
    public int getMaxFreq(){return maxFreq;}
    public static int NO_OF_COLORS = 101;
    public LinkedList <Color> colors = null;

}